package com.koron.css2.newProjectCenter.impl;


import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.vo.BaseBookListVO;
import com.koron.css2.clientCenter.bean.UserOpenBean;
import com.koron.css2.clientCenter.mapper.OpenUserMapper;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.newProjectCenter.mapper.PjInstallUserMapper;
import com.koron.css2.newProjectCenter.vo.PjInstallUserVo;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;

public class PjInstallUserSave implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {

			List<PjInstallUserVo> installUserlist = JsonUtils.objectToList(req.getData(), PjInstallUserVo.class);
			PjInstallUserMapper pjInstallUsermapper = factory.getMapper(PjInstallUserMapper.class);
			OpenUserMapper openMapper = factory.getMapper(OpenUserMapper.class);
			PjInfoMapper pjInfomapper = factory.getMapper(PjInfoMapper.class);
			String id = "";
			List<UserOpenBean> tempList = new ArrayList<UserOpenBean>();
			String strOldBillNo = "";
			for(int i=0 ; i<installUserlist.size();i++) {
				PjInstallUserVo bean = installUserlist.get(i);
				PjInfoBean pjInfoBean = pjInfomapper.selectById(bean.getPjid());
				//检查输入的册本号是否正确。
				BaseBookListVO bookVo = pjInstallUsermapper.checkBookNum(bean.getBookNo());
				if(null == bookVo || (bookVo.getBookNo()==""&&bookVo.getBookNo()==null)){
					return MessageBean.create(Constant.MESSAGE_INT_FAIL, "第"+(i+1)+"行册本号输入不正确！", null);
				}
				//根据输入的册本号修改用户册本信息，营业区域，营业所
				pjInstallUsermapper.updateArea(bookVo.getBusinessAbode(),bookVo.getBusinessArea(),bean.getUserInfoid(),bean.getBookNo());
				openMapper.userOpenRecordDeleteByPjBillNo(pjInfoBean.getBillNo());
				UserOpenBean userOpen = new UserOpenBean();
				userOpen.setCreateInfo(userInfo);
				userOpen.setUserAccountTempId(pjInfoBean.getUserAccountTempId());
				userOpen.setUserCtmTempId(pjInfoBean.getUserCtmTempId());
				userOpen.setUserInfoTempId(bean.getUserInfoid());
				userOpen.setPjInstallBillNo(pjInfoBean.getBillNo());
				//每个工程保存一条到单据上，用户列表展示
				if(!strOldBillNo.equals(pjInfoBean.getBillNo())) {

					id=userOpen.getId();
					userOpen.setBillNo(Tools.getBillNo(factory, "USER_OPEN"));
					userOpen.setBillDate(Tools.getCurrentDate());
					openMapper.userOpenAdd(userOpen);
				}
				userOpen.setUserOpenId(id);
				//修改是否立户标志
				pjInstallUsermapper.updateUserFlag(bean.getMeterId());
				tempList.add(userOpen);
				strOldBillNo = pjInfoBean.getBillNo();
			}
			openMapper.userOpenTempAddList(tempList);
			MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
			return info;
		}catch(Exception e){
			logger.error("保存立户信息失败"+e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "保存失败", null);
		}
	}

}
